package com.ruoyi.web.api;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.base.domain.BaseSupplier;
import com.ruoyi.base.service.IBaseSupplierService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/forklift")
public class forkliftController {

    @Autowired
    IBaseSupplierService baseSupplierService;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/online")
    public AjaxResult online(){
        HttpServletRequest request1 = ServletUtils.getRequest();
        String ip = IpUtils.getIpAddr(request1);

        BaseSupplier baseSupplier = baseSupplierService.getbyip(ip);
        if(ObjectUtil.isNull(baseSupplier))
            throw new ServiceException("非叉车终端");

        List<Long> list = (List<Long>)redisTemplate.opsForValue().get("onlineForkliftQueue");
        List<Long> workList = (List<Long>) redisTemplate.opsForValue().get("workForkliftQueue");
        Optional<Long> workResult = list.stream()
                .filter(item -> item == baseSupplier.getSupplierId())
                .findFirst();
        //之前的任务还没完成，不加入空闲队列
        if(workResult.isPresent())
            return AjaxResult.success();
        //工作集合里面没有，上线就进入空闲队列
        if(list == null){
            list = new ArrayList<>();
            list.add(baseSupplier.getSupplierId());
            redisTemplate.opsForValue().set("onlineForkliftQueue",list);
        }else{

            Optional<Long> result = list.stream()
                    .filter(item -> item == baseSupplier.getSupplierId())
                    .findFirst();
            if(!result.isPresent()){
                list.add(baseSupplier.getSupplierId());
                redisTemplate.opsForValue().set("onlineForkliftQueue",list);
            }
        }

        return AjaxResult.success();
    }

    @GetMapping("/outline")
    public AjaxResult outline(){
        HttpServletRequest request1 = ServletUtils.getRequest();
        String ip = IpUtils.getIpAddr(request1);

        BaseSupplier baseSupplier = baseSupplierService.getbyip(ip);
        if(ObjectUtil.isNull(baseSupplier))
            throw new ServiceException("非叉车终端");
        //离线只要从空闲队列中移除
        List<Long> list = (List<Long>)redisTemplate.opsForValue().get("onlineForkliftQueue");
        list.removeIf(value->value==baseSupplier.getSupplierId());

        redisTemplate.opsForValue().set("onlineForkliftQueue",list);
        return AjaxResult.success();
    }
}
